#pragma once
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/web/mime/multipart/Multipart.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "../dto/SimpleDto.h"
#include "../dto/CountDto.h"
#include "../dto/SceneDto.h"
#include "../dto/SceneListDto.h"
#include "../service/SceneService.h"
#include "../util/TextUtil.h"
#include "../AiBox.h"

#include OATPP_CODEGEN_BEGIN(ApiController)

/**
 * 场景控制器
 */
class SceneController : public oatpp::web::server::api::ApiController {
public:
	SceneController(const std::shared_ptr<ObjectMapper>& objectMapper)
		: oatpp::web::server::api::ApiController(objectMapper)
	{}

	/*
	* 场景添加
	*/
	ENDPOINT("POST", "/analysis/add", addScene,
		HEADER(String, authorization, "Authorization"),
		BODY_DTO(Object<SceneVo>, vo)) {

		// 返回操作结果
		auto result = SimpleDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken() !=
			TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 获取场景信息
		SceneInfo scene;
		scene.SetId(TextUtil::get_mutable_instance().GetGuid());
		scene.SetDevice(vo->deviceId.getValue(""));
		scene.SetDetectType(vo->detectType.getValue(""));
		scene.SetThreshold(vo->threshold.getValue(80));
		scene.SetModel(vo->model.getValue(""));
		scene.SetAlarmInfo(vo->alarmInfo.getValue(""));
		scene.SetAlarmCode(vo->alarmCode.getValue(""));

		// 参数校验
		if (scene.GetDevice().empty() || scene.GetDetectType().empty() ||
			scene.GetAlarmCode().empty() || scene.GetAlarmInfo().empty())
		{
			result->description = "请填写场景参数!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 新增场景信息
		SceneService sceneService;
		sceneService.Add(scene);

		// 返回操作结果
		result->error = 0;
		result->description = "success";
		result->value = scene.GetId();
		return createDtoResponse(Status::CODE_200, result);
	}

	/*
	* 场景更新
	*/
	ENDPOINT("PUT", "/analysis/update", updateScene,
		HEADER(String, authorization, "Authorization"),
		BODY_DTO(Object<SceneVo>, vo)) {

		// 返回操作结果
		auto result = SimpleDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken() !=
			TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 获取场景信息
		SceneInfo scene;
		scene.SetId(vo->id.getValue(""));
		scene.SetDevice(vo->deviceId.getValue(""));
		scene.SetDetectType(vo->detectType.getValue(""));
		scene.SetThreshold(vo->threshold.getValue(80));
		scene.SetModel(vo->model.getValue(""));
		scene.SetAlarmInfo(vo->alarmInfo.getValue(""));
		scene.SetAlarmCode(vo->alarmCode.getValue(""));

		// 参数校验
		if (scene.GetId().empty() ||
			scene.GetDevice().empty() || scene.GetDetectType().empty() ||
			scene.GetAlarmCode().empty() || scene.GetAlarmInfo().empty())
		{
			result->description = "请填写场景参数!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 更新场景
		SceneService sceneService;
		if (!sceneService.Update(scene))
		{
			result->description = "更新场景参数失败!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 返回操作结果
		result->error = 0;
		result->description = "success";
		return createDtoResponse(Status::CODE_200, result);
	}

	/*
	* 删除场景
	*/
	ENDPOINT("DELETE", "/analysis/delete", deleteScene,
		HEADER(String, authorization, "Authorization"),
		QUERY(String, id)) {

		// 返回操作结果
		auto result = SimpleDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken() !=
			TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 场景信息
		SceneInfo scene;
		scene.SetId(id.getValue(""));

		// 删除场景
		SceneService sceneService;
		sceneService.Delete(scene);

		// 返回操作结果
		result->error = 0;
		result->description = "success";
		result->value = scene.GetId();
		return createDtoResponse(Status::CODE_200, result);
	}

	/*
	* 获取场景数量
	*/
	ENDPOINT("GET", "/analysis/count", getSceneCount,
		HEADER(String, authorization, "Authorization"),
		QUERY(String, deviceId)) {

		// 返回操作结果
		auto result = CountDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken() !=
			TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 查询场景数量
		SceneService sceneService;
		int count = sceneService.Count(deviceId.getValue(""));

		// 返回操作结果
		result->error = 0;
		result->description = "success";
		result->value = count;
		return createDtoResponse(Status::CODE_200, result);
	}

	/**
	 * 查询场景列表
	 */
	ENDPOINT("GET", "/analysis/list", listScene,
		HEADER(String, authorization, "Authorization"),
		QUERY(Int32, pageIndex),
		QUERY(Int32, pageSize),
		QUERY(String, deviceId)) {

		// 返回操作结果
		auto result = SceneListDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken() !=
			TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 查询场景信息
		int index = (pageIndex.getValue(1) - 1) * pageSize.getValue(10);
		int size = pageSize.getValue(10);

		std::vector<SceneInfo> scenes;
		SceneService sceneService;
		sceneService.FindPage(index, size, deviceId.getValue(""), scenes);

		// 初始化空列表
		oatpp::List<oatpp::Object<SceneVo>> items({});

		// 填充场景列表
		for (size_t index = 0; index < scenes.size(); ++index)
		{
			auto scene = SceneVo::createShared();
			scene->id = scenes[index].GetId();
			scene->deviceId = scenes[index].GetDevice();
			scene->detectType = scenes[index].GetDetectType();
			scene->threshold = scenes[index].GetThreshold();
			scene->model = scenes[index].GetModel();
			scene->alarmInfo = scenes[index].GetAlarmInfo();
			scene->alarmCode = scenes[index].GetAlarmCode();
			scene->createTime = scenes[index].GetCreateTime().ToNormalString();
			scene->updateTime = scenes[index].GetUpdateTime().ToNormalString();
			items->emplace(items->end(), scene);
		}

		// 返回场景列表
		result->error = 0;
		result->description = "success";
		result->value = items;
		return createDtoResponse(Status::CODE_200, result);
	}
};

#include OATPP_CODEGEN_END(ApiController)